using System;

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Security.Strategy;
using DevExpress.ExpressApp.Updating;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.BaseImpl;
using DevExpress.ExpressApp.Security;
using TransLogger.Module.BusinessObjects;

namespace TransLogger.Module.DatabaseUpdate
{
    public class Updater : ModuleUpdater
    {
        public Updater(IObjectSpace objectSpace, Version currentDBVersion) : base(objectSpace, currentDBVersion) { }
        public override void UpdateDatabaseAfterUpdateSchema()
        {
            base.UpdateDatabaseAfterUpdateSchema();

            SecuritySystemRole adminRole = ObjectSpace.FindObject<SecuritySystemRole>(
        new BinaryOperator("Name", SecurityStrategy.AdministratorRoleName));
            if (adminRole == null)
            {
                adminRole = ObjectSpace.CreateObject<SecuritySystemRole>();
                adminRole.Name = SecurityStrategy.AdministratorRoleName;
                adminRole.IsAdministrative = true;
            }


            SecuritySystemRole userRole = ObjectSpace.FindObject<SecuritySystemRole>(
        new BinaryOperator("Name", "User"));
            if (userRole == null)
            {
                userRole = ObjectSpace.CreateObject<SecuritySystemRole>();
                userRole.Name = "User";
                SecuritySystemTypePermissionObject userTypePermission =
                    ObjectSpace.CreateObject<SecuritySystemTypePermissionObject>();
                userTypePermission.TargetType = typeof(SecuritySystemUser);
                SecuritySystemObjectPermissionsObject currentUserObjectPermission =
                    ObjectSpace.CreateObject<SecuritySystemObjectPermissionsObject>();
                currentUserObjectPermission.Criteria = "[Oid] = CurrentUserId()";
                currentUserObjectPermission.AllowNavigate = true;
                currentUserObjectPermission.AllowRead = true;
                userTypePermission.ObjectPermissions.Add(currentUserObjectPermission);
                userRole.TypePermissions.Add(userTypePermission);
            }

            // If a user named 'Sam' doesn't exist in the database, create this user 
            SecuritySystemUser admin = ObjectSpace.FindObject<SecuritySystemUser>(
              new BinaryOperator("UserName", "admin"));
            if (admin == null)
            {
                admin = ObjectSpace.CreateObject<SecuritySystemUser>();
                admin.UserName = "admin";
                // Set a password if the standard authentication type is used 
                admin.SetPassword("");
            }
            // If a user named 'John' doesn't exist in the database, create this user 
            SecuritySystemUser user = ObjectSpace.FindObject<SecuritySystemUser>(
                 new BinaryOperator("UserName", "user"));
            if (user == null)
            {
                user = ObjectSpace.CreateObject<SecuritySystemUser>();
                user.UserName = "user";
                // Set a password if the standard authentication type is used 
                user.SetPassword("");
            }

            // Add the "Administrators" Role to the user1 
            admin.Roles.Add(adminRole);
            // Add the "Users" Role to the user2 
            user.Roles.Add(userRole);

            BusinessObjects.Settings.GetInstance(ObjectSpace);

        }
    }
}
